/* 
 * 
 * Created on 27 Oct 2011 by simonk
 * 
 * Copyright: Multicom Products Ltd. 2011 Bristol, England
 */
package utils;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;

import objects.data.WorkPacket;

/**
 *
 * @author simonk
 */
public class PbsUtils {

	public static void createPBSfile(WorkPacket workPacket) {
		try {
			PrintWriter pw = new PrintWriter(new FileWriter(workPacket.uniqueId + File.separator + "PBS"
					+ workPacket.jobname),true);
			if(workPacket.pbsString != null) {
				pw.print(workPacket.pbsString);
				return;
			}
			pw.println("#!/bin/sh");
			pw.println("#PBS -N " + workPacket.uniqueId);
			pw.println("#PBS -j oe");
			pw.println("#PBS -o ./" + workPacket.uniqueId + "/" + workPacket.jobname + ".out");
			pw.println("#PBS -l nodes=" + workPacket.nodes + ":ppn=" + workPacket.ppn
					+ ",walltime=" + workPacket.walltime);
			pw.println("cd $PBS_O_WORKDIR");
			pw.println("cd "+ workPacket.uniqueId);
			pw.println(workPacket.executable);
			if (workPacket.nodes > 1) {
				pw.println("while read line");
				pw.println("do");
				pw.println("echo ${line%%.*} >> nodefile."+ workPacket.jobname);
				pw.println("done < $PBS_NODEFILE");
				pw.println("mpdboot -n " + workPacket.nodes + " -f nodefile."
						+ workPacket.jobname);
				pw.println("mpirun -machinefile nodefile." + workPacket.jobname
						+ " -np " + workPacket.nodes * workPacket.ppn + " "+ workPacket.executable);
				pw.println("mpdallexit");
			}
			/*} else if (ji.isMPI) {
			 * pw.println("mpirun -machinefile nodefile." + ji.jname
						+ " -n " + ji.nodes * ji.procs + " sire -n "
						+ ji.nmoves + " -r " + ji.repeat + " " + ji.filename);
				pw.println("mpdboot");
				// pw.println("cat $PBS_NODEFILE > machine.file.$PBS_JOBID");
				pw.println("mpirun -np " + ji.nodes * ji.procs + " " + ji.cmd);*/
			
			pw.close();
		} catch (Exception e) {
		}
	}

}
